﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WarrantyCheck
{
    public class WebClientEx : WebClient
    {
        public WebClientEx()
            : this(new CookieContainer())
        {

        }

        public WebClientEx(CookieContainer container)
        {
            this.container = container;
        }

        private readonly CookieContainer container = new CookieContainer();

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest r = base.GetWebRequest(address);
            var request = r as HttpWebRequest;
            if (request != null)
            {
                request.CookieContainer = container;
            }
            return r;
        }

        protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
        {
            WebResponse response = base.GetWebResponse(request, result);
            ReadCookies(request,response);
            return response;
        }

        //protected override WebResponse GetWebResponse(WebRequest request)
        //{
        //    WebResponse response = base.GetWebResponse(request);
        //    ReadCookies(response);
        //    return response;
        //}

        private void ReadCookies(WebRequest request, WebResponse r)
        {
            var response = r as HttpWebResponse;
            if (response != null)
            {
                CookieCollection cookies = response.Cookies;
                container.Add(request.RequestUri, cookies);
            }
        }
    }
    //public class CookieAwareWebClient : WebClient
    //{
    //    public CookieContainer CookieContainer { get; set; }
    //    public Uri Uri { get; set; }

    //    public CookieAwareWebClient()
    //        : this(new CookieContainer())
    //    {
    //    }

    //    public CookieAwareWebClient(CookieContainer cookies)
    //    {
    //        this.CookieContainer = cookies;
    //    }

    //    protected override WebRequest GetWebRequest(Uri address)
    //    {
    //        WebRequest request = base.GetWebRequest(address);
    //        if (request is HttpWebRequest)
    //        {
    //            (request as HttpWebRequest).CookieContainer = this.CookieContainer;
    //        }
    //        HttpWebRequest httpRequest = (HttpWebRequest)request;
    //        //httpRequest.AutomaticDecompression = c.GZip | DecompressionMethods.Deflate;
    //        return httpRequest;
    //    }

    //    protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
    //    {
    //        WebResponse response = base.GetWebResponse(request, result);
    //        HttpWebResponse
    //        String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];

    //        if (setCookieHeader != null)
    //        {
    //            //do something if needed to parse out the cookie.
    //            if (setCookieHeader != null)
    //            {
    //                Cookie cookie = new Cookie(); //create cookie
    //                this.CookieContainer.Add(request.RequestUri, cookie);
    //            }
    //        }
    //        return response;
    //    }

    //    //protected override WebResponse GetWebResponse(WebRequest request)
    //    //{
    //    //    WebResponse response = base.GetWebResponse(request);
    //    //    String setCookieHeader = response.Headers[HttpResponseHeader.SetCookie];

    //    //    if (setCookieHeader != null)
    //    //    {
    //    //        //do something if needed to parse out the cookie.
    //    //        if (setCookieHeader != null)
    //    //        {
    //    //            Cookie cookie = new Cookie(); //create cookie
    //    //            this.CookieContainer.Add(cookie);
    //    //        }
    //    //    }
    //    //    return response;
    //    //}
    //}
}
